package com.cmcc.shared.utils


import android.content.Context
import com.cmcc.shared.R
import java.text.SimpleDateFormat
import java.util.*
import kotlin.math.floor
import kotlin.math.round

/**
 * 计算时间差
 */
object TimeUtils {


    fun getTimeGap(context:Context,createTime: String): String {
        if (createTime.isNotEmpty()) {
            var format: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            var createDate: Date = format.parse(createTime)

            var nowTime: Date = Date()
            var timeGap = nowTime.time - createDate.time

            var days = floor((timeGap / (24 * 60 * 60 * 1000)).toDouble()).toInt()

            var leave1 = timeGap % (24 * 3600 * 1000)  //计算天数后剩余的毫秒数
            var hours = floor((leave1 / (3600 * 1000)).toDouble()).toInt()
            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)    //计算小时数后剩余的毫秒数
            var minutes = floor((leave2 / (60 * 1000)).toDouble()).toInt()
            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)   //计算分钟数后剩余的毫秒数
            var seconds = round((leave3 / 1000).toDouble()).toInt()

            var timeGapStr = ""
            if(days != 0)
                timeGapStr += days.toString() + context.getString(R.string.shared_time_day)
            if(hours != 0)
                timeGapStr += hours.toString() + context.getString(R.string.shared_time_hour)
            if(minutes != 0)
                timeGapStr += minutes.toString() + context.getString(R.string.shared_time_minutes)
            if(seconds != 0)
                timeGapStr += seconds.toString() + context.getString(R.string.shared_time_second)

            return timeGapStr
        }
        return ""
    }

    fun getOffTimeGap(context:Context,startTime: String):String{
        if (startTime.isNotEmpty()) {
            var format: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            var startDate: Date = format.parse(startTime)

            var nowTime: Date = Date()
            var timeGap = nowTime.time - startDate.time

            var days = floor((timeGap / (24 * 60 * 60 * 1000)).toDouble()).toInt()//计算剩余天数

            var leave1 = timeGap % (24 * 3600 * 1000)  //计算天数后剩余的毫秒数
            var hours = floor((leave1 / (3600 * 1000)).toDouble()).toInt()

            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)    //计算小时数后剩余的毫秒数
            var minutes = floor((leave2 / (60 * 1000)).toDouble()).toInt()

            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)   //计算分钟数后剩余的毫秒数
            var seconds = round((leave3 / 1000).toDouble()).toInt()

            var overTimeGap = context.getString(R.string.shared_order_has_start_time_gap)
            if(days != 0)
                overTimeGap += days.toString() + context.getString(R.string.shared_time_day)
            if(hours != 0)
                overTimeGap += hours.toString() + context.getString(R.string.shared_time_hour)
            if(minutes != 0)
                overTimeGap += minutes.toString() + context.getString(R.string.shared_time_minutes)
            if(seconds != 0)
                overTimeGap += seconds.toString() + context.getString(R.string.shared_time_second)

            return overTimeGap
        }
        return ""
    }

    fun getOverTimeGap(context:Context,startTime: String): String {
        if (startTime.isNotEmpty()) {
            var format: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            var startDate: Date = format.parse(startTime)

            var nowTime: Date = Date()
            var timeGap = startDate.time - nowTime.time

            var days = floor((timeGap / (24 * 60 * 60 * 1000)).toDouble()).toInt()//计算剩余天数

            var leave1 = timeGap % (24 * 3600 * 1000)  //计算天数后剩余的毫秒数
            var hours = floor((leave1 / (3600 * 1000)).toDouble()).toInt()

            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000)    //计算小时数后剩余的毫秒数
            var minutes = floor((leave2 / (60 * 1000)).toDouble()).toInt()

            //计算相差秒数
            var leave3 = leave2 % (60 * 1000)   //计算分钟数后剩余的毫秒数
            var seconds = round((leave3 / 1000).toDouble()).toInt()

            var temp = ""
            var overTimeGap = context.getString(R.string.shared_order_start_time_gap)
            if(days != 0)
                temp += days.toString() + context.getString(R.string.shared_time_day)
            if(hours != 0)
                temp += hours.toString() + context.getString(R.string.shared_time_hour)
            if(minutes != 0)
                temp += minutes.toString() + context.getString(R.string.shared_time_minutes)
//            if(seconds != 0)
//                temp += seconds.toString() + context.getString(R.string.shared_time_second)

            return overTimeGap + temp
        }
        return ""
    }



}